package com.wwwzh.himalayafm.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.wwwzh.himalayafm.R;
import com.wwwzh.himalayafm.adapter.recommend.GuessItemAdapter;
import com.wwwzh.himalayafm.adapter.recommend.RecommendAdapter;
import com.wwwzh.himalayafm.callback.OnDisRankImgClick;
import com.wwwzh.himalayafm.callback.OnImageClickListener;
import com.wwwzh.himalayafm.entity.ranking.DisRankHeader;
import com.wwwzh.himalayafm.fragment.DiscoveryFragment;
import com.wwwzh.himalayafm.fragment.DownloadFragment;
import com.wwwzh.himalayafm.fragment.MyInfoFragment;
import com.wwwzh.himalayafm.fragment.SubFragment;
import com.wwwzh.himalayafm.fragment.disfragment.ranking.DisRankWebFragment;
import com.wwwzh.himalayafm.fragment.disfragment.recommend.AlbumDetailFragment;
import com.wwwzh.himalayafm.fragment.myinfofrag.LoginFragment;
import com.wwwzh.himalayafm.fragment.subfragment.SubHistoryFragment;

public class XmlyActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener,OnDisRankImgClick,OnImageClickListener {


    private DiscoveryFragment discovery;
    private SubFragment sub;
    private DownloadFragment download;
    private MyInfoFragment myInfo;
    private RadioGroup radioGroup;
    private DrawerLayout drawerLayout;
    private FrameLayout fram;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xmly);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(this);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        fram = (FrameLayout) findViewById(R.id.drawer_container);
        RecommendAdapter.setOnImageClickListener(this);
        GuessItemAdapter.setOnImageClickListener(this);
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {

            }

            @Override
            public void onDrawerOpened(View drawerView) {
                //这样，点击时 ，main界面不受影响
                drawerView.setClickable(true);
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
            }

            //抽屉关闭状态下，不可滑出
            @Override
            public void onDrawerClosed(View drawerView) {
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
        initFragment();
    }

    //初始化碎片
    private void initFragment() {
        FrameLayout frame = (FrameLayout) findViewById(R.id.frameLayout);
        discovery = new DiscoveryFragment();
        sub = new SubFragment();
        download = new DownloadFragment();
        myInfo = new MyInfoFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frameLayout,discovery);
        transaction.commit();
        RadioButton button = (RadioButton) radioGroup.getChildAt(0);
        button.setChecked(true);
    }


    //radioButton 监听
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (checkedId) {
            case R.id.disBtn:
                transaction.replace(R.id.frameLayout,discovery);
                break;
            case R.id.subBtn:
                transaction.replace(R.id.frameLayout,sub);
                break;
            case R.id.downBtn:
                transaction.replace(R.id.frameLayout,download);
                break;
            case R.id.myBtn:
                transaction.replace(R.id.frameLayout,myInfo);
                break;
        }
        transaction.commit();
    }

    //“我的” 界面所有点击事件
    public void myClick(View view){


        switch (view.getId()) {
            case R.id.anchorManager:
            case R.id.msgCenter:
            case R.id.buyVoice:
            case R.id.iconMoney:
                initLoginFragment();
                drawerLayout.openDrawer(fram,true);
                break;
            case R.id.mySub:
                Toast.makeText(this, "点击了订阅", Toast.LENGTH_SHORT).show();
                break;
            case R.id.playHistory:
                initPlayHistory();
                drawerLayout.openDrawer(fram,true);
                break;

        }
    }

    //播放历史界面
    private void initPlayHistory() {
        SubHistoryFragment historyFrag = SubHistoryFragment.newInstance(1);
        getSupportFragmentManager().beginTransaction().replace(R.id.drawer_container,historyFrag).commit();
    }

    //初始化登录界面
    //不应该用add ，会重复添加，不销毁
    private void initLoginFragment() {
        LoginFragment loginFrag = new LoginFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.drawer_container,loginFrag).commit();
    }

    //推荐界面的热门推荐，小编推荐里的条目点击打开抽屉（专辑详情界面）

//    public void onImageClick(String id) {
//        Toast.makeText(this, id, Toast.LENGTH_SHORT).show();
//        AlbumDetailFragment albumFrag = new AlbumDetailFragment();
//        getSupportFragmentManager().beginTransaction().replace(R.id.drawer_container,albumFrag).commit();
//        drawerLayout.openDrawer(fram,true);
//    }

        @Override
        public void onBackPressed() {
            fram = (FrameLayout) findViewById(R.id.drawer_container);
            if (drawerLayout.isDrawerOpen(fram)) {

                drawerLayout.closeDrawers();
            } else {
                super.onBackPressed();



            }
        }

    public DrawerLayout getDrawerLayout() {
        return drawerLayout;
    }

    @Override
    public void ondisRankClicked(DisRankHeader.DisRankHeaderContent headerContent) {
        DisRankWebFragment webFragment = DisRankWebFragment.newInstance(headerContent);
        getSupportFragmentManager().beginTransaction().replace(R.id.drawer_container,webFragment).commit();
        fram = (FrameLayout) findViewById(R.id.drawer_container);
        drawerLayout.openDrawer(fram,true);

    }

    //推荐界面听单的点击事件
    @Override
    public void onImageClick(String albumId) {
        Toast.makeText(this, albumId, Toast.LENGTH_SHORT).show();
        AlbumDetailFragment albumFrag = AlbumDetailFragment.newInstance(albumId);
        getSupportFragmentManager().beginTransaction().replace(R.id.drawer_container,albumFrag).commit();
        drawerLayout.openDrawer(fram,true);
    }



    //退出界面
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode()== KeyEvent.KEYCODE_BACK) {
            showquitDialog();
        }
        return super.onKeyDown(keyCode, event);
    }

    public  void  showquitDialog(){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);

        builder.setTitle("温馨提示");

        builder.setMessage("确定退出喜马拉雅FM?");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
              finish();
            }
        });
        builder.setNegativeButton("最小化", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                moveTaskToBack(isFinishing());
            }
        });
        builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
//                  dialog.cancel();
            }
        });
        builder.setCancelable(true);
        AlertDialog dialog = builder.create();
        dialog.show();
    }

}
